﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace sau
{
    public partial class Form1 : Form
    {
        public delegate void PaintEventHandler(double[] value);
        public PaintEventHandler painterDelegate;

        ProcessLoop processLoop;

        Object syn = new Object();

        public Form1(ProcessLoop processLoop)
        {
            InitializeComponent();

            this.processLoop = processLoop;
            //processLoop.PainterEvent += new Form1.PaintEventHandler(new Form1.PaintEventHandler(this.PaintNewValue));

            ChartTheta.ChartAreas[0].AxisX.Maximum = 300;
            ChartTheta.ChartAreas[0].AxisY.Maximum = 1;
            ChartTheta.ChartAreas[0].AxisY.Minimum = -1;
            ChartTheta.Series[0].ChartType = SeriesChartType.FastLine;
            ChartTheta.Series[0].IsVisibleInLegend = true;
            ChartTheta.Series[1].ChartType = SeriesChartType.FastLine;
            ChartTheta.Series[1].IsVisibleInLegend = true;

            ChartReward.ChartAreas[0].AxisX.Maximum = 300;
            ChartReward.ChartAreas[0].AxisY.Minimum = 1.5;
            ChartReward.ChartAreas[0].AxisY.Minimum = -1.5;
            ChartReward.Series[0].ChartType = SeriesChartType.FastLine;
            ChartReward.Series[0].IsVisibleInLegend = true;
            ChartReward.Series[1].ChartType = SeriesChartType.FastLine;
            ChartReward.Series[1].IsVisibleInLegend = true;

            ChartDecision.ChartAreas[0].AxisX.Maximum = 300;
            ChartDecision.ChartAreas[0].AxisY.Maximum = 1.4;
            ChartDecision.ChartAreas[0].AxisY.Minimum = -1.4;
            ChartDecision.Series[0].ChartType = SeriesChartType.FastLine;
            ChartDecision.Series[0].IsVisibleInLegend = true;
            

            painterDelegate = new PaintEventHandler(PaintNewValue);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void ButtonStart_Click(object sender, EventArgs e)
        {
            processLoop.SetForm(this);
            processLoop.StartThread();
        }

        public void PaintNewValue(double[] value)
        {
            if (ChartTheta.Series[0].Points.Count >= 300)
            {
                ChartTheta.Series[0].Points.RemoveAt(0);
                ChartTheta.Series[1].Points.RemoveAt(0);
            }

            ChartTheta.Series[0].Points.Add(value[2]);
            ChartTheta.Series[1].Points.Add(value[3]);



            double reward = -Math.Abs(value[2] - value[3]); // -dif^2

            if (ChartReward.Series[0].Points.Count >= 300)
            {
                ChartReward.Series[0].Points.RemoveAt(0);
            }

            ChartReward.Series[0].Points.Add(reward);

            //calculate avarage reward
            int tmp = ChartReward.Series[0].Points.Count;
            if (tmp > 100)
                tmp = 100;
            double avarage = 0;
            for (int i = 0; i < tmp; i++)
            {
                avarage += ChartReward.Series[0].Points[ChartReward.Series[0].Points.Count - i - 1].YValues[0];
            }
            avarage /= tmp;
            if (ChartReward.Series[1].Points.Count >= 300)
            {
                ChartReward.Series[1].Points.RemoveAt(0);
            }
            ChartReward.Series[1].Points.Add(avarage);      

            if (ChartDecision.Series[0].Points.Count >= 300)
            {
                ChartDecision.Series[0].Points.RemoveAt(0);
            }

            ChartDecision.Series[0].Points.Add(value[4]);
        }
    }
}
